﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterpriseSuiteEcommerceCommon.DTO
{
    public class MatrixGroupItems
    {
        public int totalNumberOfItems { get; set; }
        public int itemCounter { get; set; }
        public int itemStock { get; set; }
        public string itemCode { get; set; }
        public string itemDescription { get; set; }
        public string itemName { get; set; }
        public string itemHTMLDescription { get; set; }
        public string itemUOM { get; set; }
        public string itemPrice { get; set; }
        public string images { get; set; }
        public string zoomOption { get; set; }

        public static string GetMultipleImages(string itemCode, string imageSize)
        {
            StringBuilder images = new StringBuilder();

            using (var con = DB.NewSqlConnection())
            {
                con.Open();
                using (var reader = DB.GetRSFormat(con, "SELECT FileName, IsDefaultIcon, IsDefaultMedium FROM InventoryOverrideImage with (NOLOCK) WHERE ItemCode = {0} ORDER BY ImageIndex ASC", DB.SQuote(itemCode)))
                {

                    string defaultField = string.Empty;
                    while (reader.Read())
                    {

                        ProductImage img = ProductImage.LocateMultiImage("Product", itemCode, DB.RSField(reader, "FileName"), imageSize);

                        defaultField = "IsDefaultIcon";
                        if (imageSize.ToLower() == "medium") defaultField = "IsDefaultMedium";
                        images.AppendFormat("{0} {1}::", img.src, DB.RSFieldBool(reader, defaultField));

                    }
                }
            }
            return images.ToString();
        }

        public static List<MatrixGroupItems> GetMatrixItems(string matrixGroupItemCode, int pageSize, int pageNumber, string imageSize)
        {
            var lstMatrixGroupItems = new List<MatrixGroupItems>();
            Customer thisCustomer = Customer.Current;

            using (var con = DB.NewSqlConnection())
            {
                con.Open();
                using (var reader = DB.GetRSFormat(con, "EcommerceGetMatrixItems {0}, {1}, {2}, {3}, {4}",
                            DB.SQuote(string.Empty),
                            DB.SQuote(matrixGroupItemCode),
                            pageSize,
                            pageNumber,
                            DB.SQuote(AppLogic.GetLanguageCode(thisCustomer.LocaleSetting))))
                {

                    StringBuilder tmpUOM = new StringBuilder();

                    while (reader.Read())
                    {

                        var unitOfMeasurements = ProductPricePerUnitMeasure.GetAll(DB.RSField(reader, "MatrixItemCode"), thisCustomer);

                        tmpUOM.Clear();
                        foreach (var uom in unitOfMeasurements)
                        {
                            tmpUOM.AppendFormat("{0}:-:{1}::", uom.code, uom.description);
                        }

                        lstMatrixGroupItems.Add(new MatrixGroupItems
                        {
                            totalNumberOfItems = DB.RSFieldInt(reader, "Items"),
                            itemCounter = DB.RSFieldInt(reader, "Counter"),
                            itemCode = DB.RSField(reader, "MatrixItemCode"),
                            itemName = DB.RSField(reader, "MatrixItemName"),
                            itemDescription = DB.RSField(reader, "MatrixItemDescription"),
                            itemHTMLDescription = DB.RSField(reader, "WebDescription"),
                            itemStock = InterpriseHelper.InventoryFreeStock(DB.RSField(reader, "MatrixItemCode"), thisCustomer),
                            itemUOM = tmpUOM.ToString(),
                            itemPrice = GetProductPrice(DB.RSField(reader, "MatrixItemCode")),
                            images = GetMultipleImages(DB.RSField(reader, "MatrixItemCode"), imageSize),
                            zoomOption = DB.RSField(reader, "ZoomOption")
                        });
                    }
                }
            }

            return lstMatrixGroupItems;
        }

        public static string GetProductPrice(string itemCode)
        {
            Customer ThisCustomer = Customer.Current;

            string Price = string.Empty;

            InterpriseSuiteEcommerceCommon.UnitMeasureInfo unitMeasureInfo = InterpriseHelper.GetItemDefaultUnitMeasure(itemCode);

            decimal vat = 0;
            decimal salesPrice = decimal.Zero;
            decimal promotionalPrice = decimal.Zero;
            bool withVat = AppLogic.AppConfigBool("VAT.Enabled") && ThisCustomer.VATSettingReconciled == VatDefaultSetting.Inclusive;

            salesPrice = InterpriseHelper.GetSalesPriceAndTax(ThisCustomer.CustomerCode, itemCode, ThisCustomer.CurrencyCode, decimal.One, unitMeasureInfo.Code, withVat, ref promotionalPrice, ref vat, unitMeasureInfo, ThisCustomer);

            string salesPriceFormatted = ThisCustomer.FormatBasedOnMyCurrency(salesPrice);
            string promotionalPriceFormatted = ThisCustomer.FormatBasedOnMyCurrency(promotionalPrice);

            if (promotionalPrice > decimal.Zero && promotionalPrice < salesPrice)
            {
                return promotionalPriceFormatted;
            }
            else
            {
                return salesPriceFormatted;
            }

        }
    }
}
